package com.itgaoshu.hospitalproject.service.impl;

import com.itgaoshu.hospitalproject.bean.DataGridView;
import com.itgaoshu.hospitalproject.bean.Role;
import com.itgaoshu.hospitalproject.bean.User;
import com.itgaoshu.hospitalproject.bean.UserRole;
import com.itgaoshu.hospitalproject.mapper.UserMapper;
import com.itgaoshu.hospitalproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    //查询用户的个人资料
    @Override
    public User toUpdateLogin(User user) {
        return userMapper.toUpdateLogin(user);
    }

    //在主菜单中个人资料进行修改用户信息根据id来修改
    @Override
    public int editLogin(User user) {
        return userMapper.editLogin(user);
    }

    //在主菜单的个人资料中进行修改密码以及修改盐值
    @Override
    public int editPwd(User user) {
        return userMapper.editPwd(user);
    }

    //模糊查询全部用户的方法
    @Override
    public List<User> selectAllUser(User user) {
        return userMapper.selectAllUser(user);
    }

    //检查是否存在相同用户的方法
    @Override
    public int checkUser(User user) {
        return userMapper.checkUser(user);
    }

    //添加新用户的方法
    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    //修改用户信息的方法
    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    //删除用户的方法
    @Override
    public int deleteUser(int userid) {
        return userMapper.deleteUser(userid);
    }

    //重置用户密码
    @Override
    public int resetUserPwd(User user) {
        return userMapper.resetUserPwd(user);
    }

    /**
     *
     * @param userid
     * @return
     */
    @Override
    public DataGridView queryUserRole(int userid) {
        //按照用户id查询角色
        Integer uid = userid;
        //先查询所有角色
        List<Role> allRole = userMapper.queryAllRole();
        //按照用户id查询角色
        List<Role> userRole = userMapper.queryRoleById(uid);
        //如果allRole角色的id与userRole角色的id相等那么可以查询该用户已有的角色 复选框为true
        List<Map<String,Object>> data=new ArrayList<>();
        for (Role r1 : allRole) {
            Boolean LAY_CHECKED = false;
            for (Role r2 : userRole) {
                if (r1.getRoleid() == r2.getRoleid()) {
                    LAY_CHECKED = true;
                }
            }
            Map<String, Object> map = new HashMap<String, Object>();

            map.put("roleid", r1.getRoleid());
            map.put("rolename", r1.getRolename());
            map.put("roledesc", r1.getRoledesc());
            map.put("LAY_CHECKED", LAY_CHECKED);
            data.add(map);
        }
        return new DataGridView(data);
    }

    @Override
    public void saveUserRole(UserRole userRole) {
        int userid=userRole.getUserid();
        Integer[] roleids=userRole.getIds();
        userMapper.deleteRoleUser(userid);
        //循环添加用户分配的角色
        if(roleids!=null&&roleids.length>0){
            for(Integer rid:roleids){
                userMapper.insertUserRole(userid,rid);
            }
        }
    }
}
